Imports System.ComponentModel

''' <summary>
''' Scherm om een verkoop te selecteren
''' </summary>
Public Class SelecteerVerkoopForm

#Region "Private declarations"

    Private _databaseBestand As String

#End Region

#Region "Public property's"

    Public ReadOnly Property IdVerkoop() As Integer
        Get
            If (Grid1.SelectedRows.Count = 1) Then
                Dim gridRow As DataGridViewRow = Grid1.SelectedRows(0)
                Dim dataRow As ArtikelsDataSet.VerkopenRow = DirectCast(DirectCast(gridRow.DataBoundItem, DataRowView).Row, ArtikelsDataSet.VerkopenRow)
                If (dataRow IsNot Nothing) Then
                    Return dataRow.Id
                Else
                    Return 0
                End If
            Else
                Return 0
            End If
        End Get
    End Property


#End Region

#Region "Constructors"

    Public Sub New(ByVal databaseBestand As String)
        Me.InitializeComponent()
        _databaseBestand = databaseBestand
        Me.BindingNavigatorAddNewItem.Visible = False
        Me.BindingNavigatorSaveItem.Visible = False
        Me.BindingNavigator1.AddNewItem = Nothing
        Me.BindingNavigator1.DeleteItem = Nothing
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        Try
            Dal.VerkoopOphalen(Me.ArtikelsDataSet1, _databaseBestand)
            InitGrid()
        Catch ex As Exception
            MsgBox.ShowError("Fout tijdens het inladen van de gegevens")
            log.WriteError("SelecteerLeveringForm_Load" & Environment.NewLine & ex.ToString())
        End Try
    End Sub

#End Region

#Region "Event handling"

    Private Sub Grid1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles Grid1.CellFormatting
        If (e.ColumnIndex = Grid1.Columns("AantalDetails").Index) Then
            e.Value = String.Format("{0} detail(s)", e.Value)
        End If
    End Sub

    Private Sub Grid1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Grid1.SelectionChanged
        btnOK.Enabled = (Grid1.SelectedRows.Count = 1)
    End Sub

    Private Sub Grid1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Grid1.CellDoubleClick
        Me.DialogResult = Windows.Forms.DialogResult.OK ' we hebben een selectie
        Me.Close()
    End Sub

#End Region

#Region "Helperfuncties"

    Private Sub InitGrid()
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.IdColumn.ColumnName, Grid1)
            .Visible = False ' Autonumber niet tonen
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.IdSoortColumn.ColumnName, Grid1)
            .Width = 100
            .HeaderText = "Soort"
            Dim column As DataGridViewComboBoxColumn = DirectCast(DataGridViewHelper.GetGridColumnByDataName _
                (ArtikelsDataSet1.Verkopen.IdSoortColumn.ColumnName, Grid1), DataGridViewComboBoxColumn)
            column.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing ' verberg de uitklappijl
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.IdWinkelierColumn.ColumnName, Grid1)
            .Width = 100
            .HeaderText = "Winkelier"
            Dim column As DataGridViewComboBoxColumn = DirectCast(DataGridViewHelper.GetGridColumnByDataName _
                (ArtikelsDataSet1.Verkopen.IdWinkelierColumn.ColumnName, Grid1), DataGridViewComboBoxColumn)
            column.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing ' verberg de uitklappijl
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.DatumColumn.ColumnName, Grid1)
            .Width = 100
            .HeaderText = "Datum"
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.BegintotaalColumn.ColumnName, Grid1)
            .Width = 100
            .HeaderText = "Begintotaal"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.Format = "c"
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.EindtotaalColumn.ColumnName, Grid1)
            .Width = 100
            .HeaderText = "Eindtotaal"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.Format = "c"
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.InUitKasBedragColumn.ColumnName, Grid1)
            .Width = 160
            .HeaderText = "Bedrag in/uit kas"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.Format = "c"
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.NaarBankBedragColumn.ColumnName, Grid1)
            .Width = 160
            .HeaderText = "Bedrag naar bank"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .DefaultCellStyle.Format = "c"
        End With
        With DataGridViewHelper.GetGridColumnByDataName(ArtikelsDataSet1.Verkopen.AantalDetailsColumn.ColumnName, Grid1)
            .Width = 140
            .HeaderText = "Aantal details"
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        End With
    End Sub

#End Region

End Class